package com.das.mechanic_base.mvp.view.show;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayerStandard;
import uk.co.senab.photoview.PhotoView;

@Route(path = X3AppRouterConstants.Router.SHOW_IMAGE)
public class X3ShowImageActivity extends X3BaseActivity {

    @BindView(R2.id.tv_num)
    TextView tv_num;
    @BindView(R2.id.vp_image)
    ViewPager vp_image;
    private List<String> imageList;
    private MyPageAdapter myPageAdapter;

    private List<JZVideoPlayerStandard> mList;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_activity_show_image;
    }

    @Override
    public boolean isTransparent() {
        return true;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    protected void initView() {
        if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        swipeBackLayout.setEnableGesture(false);

        mList = new ArrayList<>();

        imageList = (List<String>) getIntent().getSerializableExtra("imageList");

        int index = getIntent().getIntExtra("index", 0);

        tv_num.setText((index + 1) + "/"+imageList.size());

        myPageAdapter = new MyPageAdapter();
        vp_image.setAdapter(myPageAdapter);
        vp_image.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_num.setText((position + 1) + "/" +imageList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp_image.setCurrentItem(index);
    }

    @Override
    protected X3BasePresenter createPresenter() {
        return null;
    }

    @OnClick(R2.id.iv_back)
    public void onViewClick(View view) {
        if (view.getId() == R.id.iv_back) {
            finish();
        }
    }

    class MyPageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = View.inflate(X3ShowImageActivity.this, R.layout.x3_all_image_item,null);
            PhotoView iv_img = view.findViewById(R.id.iv_image);
            JZVideoPlayerStandard jz_view = view.findViewById(R.id.jz_view);

            mList.add(jz_view);
            String path = imageList.get(position);
            if (!X3StringUtils.isEmpty(path) && !path.contains("storage")) {
                path = X3StringUtils.getImageUrl(path);
            }

            if (path.endsWith("mp4")) {
                iv_img.setVisibility(View.GONE);
                jz_view.setVisibility(View.VISIBLE);
                X3GlideNewUtils.loadNormalNoCropImage(X3ShowImageActivity.this, path,jz_view.thumbImageView, Color.BLACK);
                jz_view.backButton.setVisibility(View.GONE);
                jz_view.setUp((path.contains("http://")) ? X3MyApp.getInstance().getVideoCacheProxy(X3ShowImageActivity.this).getProxyUrl(path) : path,JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,"");
                jz_view.startVideo();

            }else {
                iv_img.setVisibility(View.VISIBLE);
                jz_view.setVisibility(View.GONE);
                X3GlideNewUtils.loadNormalNoCropImage(X3ShowImageActivity.this, path,iv_img,Color.BLACK);
            }

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayerStandard.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("图片预览页");
        JZVideoPlayerStandard.releaseAllVideos();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("图片预览页");
    }
}